import java.util.*;
import java.io.*;

public class 哈夫曼编码 {


    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    public static void main(String[] args) throws IOException {
        int n = in.nextInt();
        PriorityQueue<Long> heap = new PriorityQueue<>();
        long ans = 0;
        long sum = 0;
        for (int i = 0;i < n;i++) {
            heap.add(in.nextLong());
        }
        if (n == 1) {
            out.println(heap.poll());
            return;
        }
        while (heap.size() > 1) {
            long a = heap.poll();
            long b = heap.poll();
            ans += a + b;
            sum = a + b;
            heap.add(sum);
        }
        out.println(ans);
        out.close();
    }

    public static class Read {
        StringTokenizer st = new StringTokenizer("");
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String next() throws IOException {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(bf.readLine());
            }
            return st.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }
    }
}
